<!-- 护理等级列表页 -->
<template>
  <div class="min-h serveGrade bg-wt">
    <!-- 组件整合，其他组件需要定义在这个div内部 -->
    <SearchForm
      :search-data="pagination"
      @handle-clear="handleClear"
      @handle-search="handleSearch"
      @handle-reset="handleReset"
    ></SearchForm>

    <TableList
      :list-data="listData"
      :pagination="pagination"
      :total="total"
      @get-current="getCurrent"
      @handle-edit="handleEdit"
      @handle-bulid="handleBulid"
      @handle-click-delete="handleClickDelete"
      @handle-forbidden="handleForbidden"
      @handleLook="handleLook"
    ></TableList>
    <DialogFrom
      ref="formRef"
      :visible="visible"
      :title="title"
      :data="formBaseData"
      :plan-data="planData"
      @handle-close="handleClose"
      @handle-add="handleAdd"
      @handle-edit-form="handleEditForm"
    >
    </DialogFrom>
    <Forbidden
      :visible="dialogVisible"
      :text="operateText"
      @handle-submit="handleForbiddenSub"
      @handle-close="handleForbiddenClose"
    ></Forbidden>

    <Delete
      :visible="dialogDeleteVisible"
      :delete-text="operateText"
      @handle-delete="handleDelete"
      @handle-close="handleDeleteClose"
    ></Delete>
  </div>
</template>

<script setup lang="ts">

import SearchForm from "@/pages/serve/plan/grade/components/SearchForm.vue";
import TableList from "@/pages/serve/plan/grade/components/TableList.vue";
import DialogFrom from "@/pages/serve/plan/grade/components/DialogFrom.vue";

import { onMounted, ref } from "vue";
import {
  getLevelAllList,
  getLevelDetails,
  getLevelList, getnursAllPlanList,
  levelAdd,
  levelDelete,
  levelStatus,
  levelUpdate
} from "@/api/serve";
import { MessagePlugin } from "tdesign-vue-next";
import Forbidden from "@/components/Forbidden/index.vue";
import Delete from "@/components/OperateDialog/index.vue";

const visible = ref(false) // 新增，编辑弹窗
const listData = ref([]) // 列表数据
const dataLoading = ref(false) // 加载中
const formBaseData = ref({}) // 弹窗表单内容
const title = ref('') // 弹窗标题
const dialogDeleteVisible = ref(false) // 控制删除弹层显示隐藏
const dialogVisible = ref(false) // 禁用弹窗
const typeId = ref('') // 设置删除id
const typeStatus = ref(null) // 禁用启用
const operateText = ref('护理计划') // 要操作的内容提示
const statusText = ref('') // 启用禁用提示
const total = ref(0) // 总条数
const planData = ref([]) // 护理计划数据
const formRef = ref(null)
const lookVisible = ref(false)

onMounted(() => {
  getList()
  getPlan()
})

// 分页
const pagination = ref<Object | any>({
  pageSize: 10,
  pageNum: 1, // 默认当前页
  name: '', // 默认值为空
  status: undefined,
})

// 清空搜索表单
const handleClear = (v) => {
  if (v === 'name') {
    delete pagination.value.name
  } else {
    delete pagination.value.status
  }
  pagination.value = {
    ...pagination.value
  }
  getList()
}

// ------定义方法------
// 获取列表数据
const getList = async () => {
  dataLoading.value = false
  try {
    const res: any = await getLevelList(pagination.value) // 获取列表数据
    listData.value = res.data.records
    total.value = Number(res.data.total)
  } finally {
    dataLoading.value = false
  }
}

// 搜索功能
const handleSearch = () => {
  if (pagination.value.name !== undefined || pagination.value.status !== undefined) {
    pagination.value.pageNum = 1;
  }
  getList()
  MessagePlugin.info('搜索中...', 500)
}

// 重置，清空搜索框
const handleReset = () => {
  // 重置页码
  pagination.value = {
    pageSize: 10,
    pageNum: 1
  }
  getList()
}

// 翻页设置当前页
const getCurrent = (val) => {
  pagination.value.pageNum = val.current
  pagination.value.pageSize = val.pageSize
  getList()
}

// 编辑
const handleEdit = (val) => {
  // 获取详情
  getDetails(val.id)
  // 显示弹窗
  visible.value = true
  // 将弹窗的标题
  title.value = '编辑'
}

// 获取详情数据
const getDetails = async (id) => {
  const res: any = await getLevelDetails(id) // 获取列表数据
  if (res.code === 200) {
    formBaseData.value = res.data
  }
}

// 新增房型
const handleBulid = () => {
  // 显示弹窗
  title.value = '新增'
  visible.value = true
}

// 点击删除
const handleClickDelete = (val) => {
  typeId.value = val.id
  dialogDeleteVisible.value = true
}

// 禁用弹窗
const handleForbidden = (val) => {
  typeId.value = val.id
  if (val.status === 1) {
    dialogVisible.value = true
    typeStatus.value = 0
    statusText.value = '禁用成功'
  } else {
    typeStatus.value = 1
    handleForbiddenSub()
    statusText.value = '启用成功'
  }
}

// 确定禁用
const handleForbiddenSub = async () => {
  const params = {
    id: typeId.value,
    status: typeStatus.value
  }
  const res: any = await levelStatus(params)
  if (res.code === 200) {
    dialogVisible.value = false
    MessagePlugin.success(statusText.value)
    getList()
  }
}

const handleForbiddenClose = () => {
  dialogVisible.value = false
}

// 查看
const handleLook = (id) => {
  lookVisible.value = true
  getDetails(id)
}

// 添加
const handleAdd = async (val) => {
  const res: any = await levelAdd(val)
  if (res.code === 200) {
    MessagePlugin.success('添加成功')
    getList()
    handleClose()
    formRef.value.handleClear()
  }
}

// 关闭弹窗
const handleClose = () => {
  visible.value = false // 关闭新增弹窗
  getList()
}

// 获取所有护理计划列表
const getPlan = async () => {
  const res: any = await getLevelAllList();

  const allPlan: any = await getnursAllPlanList();

  if (res.code === 200 && allPlan.code === 200) {
    res.data.forEach((item) => {
      const index = allPlan.data.findIndex(it => item.planId === it.id);
      if (index !== -1) {
        allPlan.data[index].disabled = true;
      }
    })
    planData.value = allPlan.data;
  }
}

// 编辑
const handleEditForm = async (val) => {
  const res: any = await levelUpdate(val)
  if (res.code === 200) {
    MessagePlugin.success('编辑成功');
    getPlan();
    getList();
    handleClose()
    formRef.value.handleClear()
  }
}

// 确认删除
const handleDelete = async () => {
  const res: any = await levelDelete(typeId.value)
  if (res.code === 200) {
    dialogDeleteVisible.value = false
    MessagePlugin.success('删除成功')
    getPlan();
    getList()
  }
}

// 关闭删除弹层
const handleDeleteClose = () => {
  dialogDeleteVisible.value = false
}
</script>
